import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin

from tools import common_assert, read_json, clear_data

from time import sleep


class TestRegisterLogin:
    def setup_class(self):
        log.info("正在初始化数据库数据")
        # 重点：只能使用类方法清除数据?
        clear_data()
        log.info("初始化数据完成！")
    # 初始化
    def setup_method(self):
        # 获取session对象
        log.info("正在获取session对象")
        self.session = requests.session()
        # 获取ApiRegisterLogin实例对象
        log.info("正在获取ApiRegisterLogin对象")
        self.api = ApiRegisterLogin(self.session)

    # 结束
    def teardown_method(self):
        # 关闭session对象
        log.info("正在关闭session对象")
        self.session.close()

    # 1、图片验证码接口 测试
    @pytest.mark.parametrize("random,expect_code", read_json("register_login.json", "img_code"))
    def test01_img_code(self, random, expect_code):
        try:
            result = self.api.api_img_code(random=random)
            log.info("图片验证码接口测试结果为:{}".format(result.status_code))
            # 断言
            common_assert(response=result, status_code=expect_code)
            log.info("图片验证码接口断言通过")

        except Exception as e:
            # 捕获异常的目的是记录日志
            log.error(e)
            # 抛出异常
            raise

    # 2、短信验证码接口 测试
    @pytest.mark.parametrize("phone,imgverifycode,expect_json", read_json("register_login.json", "phone_code"))
    def test02_phone_code(self, phone, imgverifycode, expect_json):
        try:
            # 1、获取图片验证码
            self.api.api_img_code(123123)
            # 2、获取短信验证码
            result = self.api.api_phone_code(phone=phone, imgVerifyCode=imgverifycode)
            log.info("短信验证码接口测试结果为:{}".format(result.text))
            # 3、断言
            common_assert(result, expect_json=expect_json)
            log.info("短信验证码接口断言通过")
        except Exception as e:
            # 捕获异常的目的是记录日志
            log.error(e)
            # 抛出异常
            raise

    # 3、注册接口 测试
    @pytest.mark.parametrize("phone,password,imgverifycode,phone_code,expect_json", read_json("register_login.json", "register"))
    def test03_register(self, phone, password, imgverifycode, phone_code, expect_json, invite_phone=None):
        try:
            # 1、获取图片验证码
            self.api.api_img_code(123123)
            # 2、获取短信验证码
            self.api.api_phone_code(phone=phone, imgVerifyCode=imgverifycode)
            # 3、注册
            result = self.api.api_register(phone=phone, password=password, verifycode=imgverifycode, phone_code=phone_code, invite_phone=invite_phone)
            log.info("注册接口测试结果为:{}".format(result.text))
            # 4、断言
            common_assert(result, expect_json=expect_json)
            log.info("注册接口断言通过")
        except Exception as e:
            # 捕获异常的目的是记录日志
            log.error(e)
            # 抛出异常
            raise

    # 4、登录接口 测试
    @pytest.mark.parametrize("keywords,password,expect_json", read_json("register_login.json", "login"))
    def test04_login(self, keywords, password, expect_json):
        try:
            # 通过password判断是否是 ”锁定“用例
            if "error" not in password:
                # 不是 ”锁定“ 用例
                # 1、登录
                result = self.api.api_login(keywords=keywords, password=password)
                log.info("登录接口测试结果为:{}".format(result.text))
                # 2、断言
                common_assert(result, expect_text=expect_json)
                log.info("登录接口断言通过")
            else:
                # "锁定"用例,循环3次
                i = 1
                result = None
                while i <= 3:
                    result = self.api.api_login(keywords=keywords, password=password)
                    i += 1
                # 判断是否已经锁定，锁定则等待60秒
                assert "锁定" in result.text
                log.info("登录接口“锁定”测试结果为:{}".format(result.text))
                sleep(60)
                # 再次登录
                result = self.api.api_login(keywords=13600001111, password="test123")
                common_assert(result, expect_text=expect_json)
                log.info("登录接口断言通过")
        except Exception as e:
            # 捕获异常的目的是记录日志
            log.error(e)
            # 抛出异常
            raise

    # 5、查询登录状态接口 测试
    @pytest.mark.parametrize("status,expect_json", read_json("register_login.json", "login_status"))
    def test05_login_status(self, status, expect_json):
        try:
            if status == "登录":
                self.api.api_login(keywords=13600001111, password="test123")
            # 调用登录查询接口
            result = self.api.api_login_status()
            log.info("登录状态接口测试结果为:{}".format(result.text))
            # 断言
            common_assert(result, expect_json=expect_json)
            log.info("登录状态接口断言通过")
        except Exception as e:
            # 捕获异常的目的是记录日志
            log.error(e)
            # 抛出异常
            raise
